<?php
/**
 * 教师约课记录
 * @author 鸣枫
 *
 */
error_reporting(E_ALL ^ E_NOTICE);
class Tcourserecord extends CT_Controller{
    
    private $teacher_id;     //教师ID
    
    function __construct() {
        parent::__construct();
        
        $this->teacher_id = $this->_uid;
        
        if(!$this->teacher_id && !$this->input->is_ajax_request()){
            redirect('/login');
        }
    }
    
    /**
     * 约课列表
     */
    public function index(){       
        try {
            //自动确认上课
            $this->AutoClassBegins();
            //超过3天不点击下课自动下课
            $this->AutoClassEnd();
            
            $type = intval($this->input->get('type'));    //类型
            $this->load->helper("arraytool");   //加载工具
            $this->load->model('Book_model');
            $fild = " (case `status` when 3 then 7 else status end) as ob_status,id as book_id,member_id,teacher_id,book_member_id,course_package_id,status,book_date,book_date_start";
            $where = " teacher_id = {$this->teacher_id} and nursery_id ={$this->_nursery_id} ";
            $order_by = "ob_status asc, update_date desc" ;
            switch ($type){
                case 1:     //等待上课
                    $where .= ' AND status = 0 ';
                    break;
                case 2:     //已经下课
                    $where .= ' AND status in(2,6) ';
                    break;
                default:
                    $where .= ' AND status != 4 ';
                    break;
            }
            
            $book_list = $this->Book_model->findAll($where,$fild,$order_by);
            if($book_list){
            
                //获取课包信息
                $this->load->model('Course_Package_model');
                $fild = 'id,title as book_title,logo as book_logo';
                $where = " id IN ( ". filterValue($book_list,'course_package_id',true) ." )";
                $course_package_list = $this->Course_Package_model->findAll($where,$fild);
                $course_package_list = restByKey($course_package_list,'id');
                $book_list = arrayMerge($book_list,'course_package_id',$course_package_list);   //合并
                            
                //获取约课用户信息
                $this->load->model('Book_Member_model');
                $fild = 'id as address_id, baby_name,linkman,link_mobile,address';
                $where = " id IN ( " . filterValue($book_list,'book_member_id',true) .")";
                $book_member_list = $this->Book_Member_model->findAll($where,$fild);
                $book_member_list = restByKey($book_member_list,'address_id');
                $book_list = arrayMerge($book_list,'book_member_id',$book_member_list);   //合并
                
                
                //获取用户信息
                $this->load->model('Member_model');
                $fild = 'id as member_id, wx_avatar as baby_avatar';
                $where = " id IN ( " . filterValue($book_list,'member_id',true) .")";
                $member_list = $this->Member_model->findAll($where,$fild);
                $member_list = restByKey($member_list,'member_id');
                $book_list = arrayMerge($book_list,'member_id',$member_list);   //合并
            }
            
            $this->ct_template->write('_title', '约课记录');
            $this->ct_template->write_view('content', 'courserecord/tindex',array(
                'book_list'    =>  $book_list,
                'weekArr'    =>  array("周日","周一","周二","周三","周四","周五","周六"),
                'type'  =>  $type,
            ), FALSE);
            $this->ct_template->render();
            
        }catch (Exception $e){
            $this->ct_template->write('_title', '错误提示');
            $this->ct_template->write_view('content', 'errors/html/warn', array('msg'=>$e->getMessage() ), FALSE);
            $this->ct_template->render();
        }
        
    }
    
    /**
     * 自动确认上课
     */
    public function AutoClassBegins(){
        $this->load->model('Book_model');
        $where = ' teacher_id = '.$this->teacher_id;
        $where.= ' AND status = 0 ';
        $where.= " AND book_date <= '" . date('Y-m-d') ."'";
        $where.= " AND book_date_start <= '" .date('H:i:s')."'";
        
        $book_list = $this->Book_model->update(array('status'=>1),$where);
    }
    
    /**
     * 超过3天不点击下课自动下课
     */
    public function AutoClassEnd(){
        $this->load->model('Book_model');
        $where = ' teacher_id = '.$this->teacher_id;
        $where.= ' AND status in(1,5,6) ';
        $where.= ' AND UNIX_TIMESTAMP(create_date)+60*60*24*3 <='.time().' ';
    
        $this->Book_model->update(array('status'=>2),$where);
    }
    
    /**
     * 下课
     */
    public function classover(){
        try {
            $book_id = intval($this->input->get('book_id'));    //约课ID
            if(empty($book_id))throw new Exception('约课ID为空');
    
            $this->load->model('Book_model');
            $fild = 'id,member_id,teacher_id,course_package_id,status';
            $where = array();
            $where['id'] = $book_id;
            $where['teacher_id'] = $this->teacher_id;
            $where['status in(1,5)'] = NULL;
            $book_info = $this->Book_model->find($where,$fild);
            if(empty($book_info))throw new Exception('没有约课记录');
            
            //查询是否已评价过
            $this->load->model('Book_Evaluate_model');
            $fild = 'id';
            $where1 = array();
            $where1['status'] = 1;
            $where1['book_id'] = $book_id;
            $where1['member_id'] = $book_info['member_id'];
            $book_evaluate_info = $this->Book_Evaluate_model->find($where1,$fild);
    
            //修改状态
            $data = array();
            if($book_evaluate_info){
                $data['status'] = 3;
            }else if($book_info['status'] == 1){
                $data['status'] = 6;
            }else{
                $data['status'] = 2;
            }
            $data['update_date'] = date('Y-m-d H:i:s');
            $bool = $this->Book_model->update($data,$where);
            if(empty($bool))throw new Exception('修改失败');
    
            $this->ct_template->write('_title', '取消');
            $this->ct_template->write_view('content', 'courserecord/tclassover',array(
                'book_id' => $book_id,
            ), FALSE);
            $this->ct_template->render();
    
        }catch (Exception $e){
            $this->ct_template->write('_title', '错误提示');
            $this->ct_template->write_view('content', 'errors/html/warn', array('msg'=>$e->getMessage() ), FALSE);
            $this->ct_template->render();
        }
    
    }
    
    /**
     * 约课详情
     */
    public function courseinfo(){
        try {
            $book_id = intval($this->input->get('book_id'));    //约课ID
            if(empty($book_id))throw new Exception('约课ID为空');
    
            //$this->load->helper("arraytool");   //加载工具
            $this->load->model('Book_model');
            $fild = 'id as book_id,book_member_id,member_id,teacher_id,course_package_id,status,book_date,book_date_start,hours,order_id,message,teacher_msg';
            $where = array();
            $where['id'] = $book_id;
            $where['teacher_id'] = $this->teacher_id;
            $book_info = $this->Book_model->find($where,$fild);
            if(empty($book_info))   throw new Exception('没有约课记录');
    
            //获取课包信息
            $this->load->model('Course_Package_model');
            $fild = 'id,title as book_title,logo as book_logo';
            $where = array();
            $where['id'] = $book_info['course_package_id'];
            $course_package_info = $this->Course_Package_model->find($where,$fild);
    
            //获取用户信息
            $this->load->model('Book_Member_model');
            $fild = 'id,baby_name,linkman,link_mobile,address';
            $where = array();
            $where['id'] = $book_info['book_member_id'];
            $book_member_info = $this->Book_Member_model->find($where,$fild);
            
            //获取用户信息
            $this->load->model('Member_model');
            $fild = 'id as member_id, wx_avatar as baby_avatar';
            $where = array();
            $where['id'] = $book_info['member_id'];
            $member_list = $this->Member_model->find($where,$fild);
    
            $book_info = array_merge($book_info,$course_package_info,$book_member_info,$member_list);  //合并
    
            $book_info['evaluate'] = $this->evaluatelist(); //获取评价列表
    
            $this->ct_template->write('_title', '约课详情');
            $this->ct_template->write_view('content', 'courserecord/tinfo',array(
                'book_info'    =>  $book_info,
                'weekArr'    =>  array("周日","周一","周二","周三","周四","周五","周六"),
                'type'  =>  array('等待上课','上课中','已下课','已结束','已取消'),
            ), FALSE);
            $this->ct_template->render();
    
        }catch (Exception $e){
            $this->ct_template->write('_title', '错误提示');
            $this->ct_template->write_view('content', 'errors/html/warn', array('msg'=>$e->getMessage() ), FALSE);
            $this->ct_template->render();
        }
    
    }
    /**
     * 获取评价列表
     */
    private function evaluatelist(){
        try {
            $book_id = intval($this->input->get('book_id'));   //约课ID
            if(empty($book_id))throw new Exception('约课ID为空');
    
    
            $this->load->model('Book_Evaluate_model');
            $fild = 'id,star,message,create_date';
            $where = array();
            $where['status'] = 1;
            $where['book_id'] = $book_id;
            $where['teacher_id'] = $this->teacher_id;
    
            $book_evaluate_list = $this->Book_Evaluate_model->findAll($where,$fild);
            if(empty($book_evaluate_list))throw new Exception('没有评论列表');
    
            return $book_evaluate_list;
        }catch (Exception $e){
            return array();
        }
    }
    
    /**
     * 提交备注
     */
    public function evaluate(){
        try {
            $book_id = intval($this->input->get('book_id'));    //约课ID
            if(empty($book_id))throw new Exception('约课ID为空');
            $message = $this->input->get('message',TRUE);    //内容
            if(empty($message))throw new Exception('备注为空');
    
            $this->load->model('Book_model');
            $fild = 'id,teacher_id,course_package_id,status';
            $where = array();
            $where['id'] = $book_id;
            $where['teacher_id'] = $this->teacher_id;
            $book_info = $this->Book_model->find($where,$fild);
            if(empty($book_info) || !in_array($book_info['status'],array(2,6)))
                throw new Exception('没有约课记录,或课程未结束');
            
            if($book_info){
                //修改状态
                $where = array();
                $where['id'] = $book_id;
                $where['teacher_id'] = $this->teacher_id;
    
                $data = array();
                $data['teacher_msg'] = $message;
                $data['update_date'] = date('Y-m-d H:i:s');
                $bool = $this->Book_model->update($data,$where);
                if(empty($bool))throw new Exception('添加备注失败');
            }
    
            redirect("/tcourserecord/");
    
        }catch (Exception $e){
            $this->ct_template->write('_title', '错误提示');
            $this->ct_template->write_view('content', 'errors/html/warn', array('msg'=>$e->getMessage() ), FALSE);
            $this->ct_template->render();
        }
    
    }
    
    /**
     * 取消预约
     */
    public function classcancel(){
        try {
            $book_id = intval($this->input->get('book_id'));    //约课ID
            if(empty($book_id))throw new Exception('约课ID为空');
    
            $this->load->model('Book_model');
            $fild = 'id,teacher_id,course_package_id,status';
            $where = array();
            $where['id'] = $book_id;
            $where['teacher_id'] = $this->teacher_id;
            $where['status'] = 0;
            $book_info = $this->Book_model->find($where,$fild);
            if(empty($book_info))throw new Exception('没有约课记录');
    
            //修改状态
            $data = array();
            $data['status'] = 4;
            $data['update_date'] = date('Y-m-d H:i:s');
            $bool = $this->Book_model->update($data,$where);
            if(empty($bool))throw new Exception('修改失败');
    
            //课时增加
            $this->load->model('Order_model');
            $sql = "update `order` set `surplus_hours` = `surplus_hours` + {$book_info['hours']} where id = '{$book_info['order_id']}' ";
            $this->Order_model->query($sql);
    
            //预约列表减少预约总数
            $this->load->model('Course_package_model');
            $course_package_info = $this->Course_package_model->find(array('id'=>$book_info['course_package_id']));
            if($course_package_info){
                $this->load->model('Course_list_model');
                $sql = "update `course_list` set `total_book` = `total_book` - {$book_info['hours']} where id = '{$course_package_info['course_list_id']}' ";
                $this->Course_list_model->query($sql);
            }
    
            //减少老师上课次数
            $this->load->model('Teacher_model');
            $sql = "update `teacher` set `book_num` = `book_num` - {$book_info['hours']} where id = '{$book_info['teacher_id']}' ";
            $this->Teacher_model->query($sql);
    
            $this->ct_template->write('_title', '取消');
            $this->ct_template->write_view('content', 'courserecord/classcancel',array(
    
            ), FALSE);
            $this->ct_template->render();
    
        }catch (Exception $e){
            $this->ct_template->write('_title', '错误提示');
            $this->ct_template->write_view('content', 'errors/html/warn', array('msg'=>$e->getMessage() ), FALSE);
            $this->ct_template->render();
        }
    
    }
    
}










